#include "buttondelegate.h"

ButtonDelegate::ButtonDelegate(QObject *parent) : QStyledItemDelegate(parent) {}

QWidget *ButtonDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                                      const QModelIndex &index) const{
    QWidget *editor = new QWidget(parent);
    QHBoxLayout *layout = new QHBoxLayout(editor);
    layout->setContentsMargins(0, 0, 0, 0);

    QPushButton *editButton = new QPushButton("修改", editor);
    QPushButton *deleteButton = new QPushButton("删除", editor);

    layout->addWidget(editButton);
    layout->addWidget(deleteButton);

    connect(editButton, &QPushButton::clicked, this, [=]() { emit editClicked(index); });
    connect(deleteButton, &QPushButton::clicked, this, [=]() { emit deleteClicked(index); });
    editor->show();
    return editor;
}

void ButtonDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    // No data to set in this example
}

void ButtonDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    // No data to set in this example
}

void ButtonDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    editor->setGeometry(option.rect);
}
